/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Truck;

/**
 *
 * @author MaleneLykke
 */
public class TruckHandler {

    private DatabaseConnection db;

    public TruckHandler(DatabaseConnection db) {
        this.db = db;
    }

    public ArrayList<Truck> getTrucks() throws SQLException {
        ArrayList<Truck> list = new ArrayList<>();

        String sql = "select id, truck_name, pallets, volume, max_weight, special from truck";
        ResultSet rs = db.selectQuery(sql);
        while (rs.next()) {
            String str = rs.getBoolean("special") ? "Ja" : "Nej";
            //^^Conditional operator. Hvis True Udskriv Ja, hvis false udskriv Nej.
            list.add(new Truck(rs.getInt("id"), rs.getString("truck_name"), rs.getInt("pallets"), 
                    rs.getInt("volume"), rs.getInt("max_weight"), str));

        }
        return list;
    }

    public ArrayList<Truck> getValidTrucks(String date, int weight, int volume, int pallet, boolean special) throws SQLException {
        ArrayList<Truck> list = new ArrayList<>();

        String sql = "select id, truck_name, pallets, volume, max_weight, special from truck "
                + "where id not in (select truck_id from shipment where shipment_date = '" + date + "') "
                + "and max_weight >= " + weight + " and volume >= " + volume + " and pallets >= " + pallet
                + " and special = " + special;
        ResultSet rs = db.selectQuery(sql);
        while (rs.next()) {
            list.add(new Truck(rs.getInt("id"), rs.getString("truck_name"), rs.getInt("pallets"), 
                    rs.getInt("volume"), rs.getInt("max_weight"), rs.getBoolean("special")));
        }
        return list;
    }

}
